package com.whh.permission.controller;


import com.whh.permission.po.Dept;
import com.whh.permission.service.DeptService;
import com.whh.permission.util.JsonObject;
import com.whh.permission.util.R;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
public class DeptController {
    @Resource
    private DeptService deptServiceImpl;

    @RequestMapping("dept/deptAll")
    @ResponseBody
    public JsonObject queryDeptAll(){
        List<Dept> list = deptServiceImpl.queryDeptAll();
        JsonObject object = new JsonObject();
        object.setCode(0);
        object.setMsg("okok");
        object.setCount((long) list.size());
        object.setData(list);
        return object;
    }

    @RequestMapping("/dept")
    public String deptIndex(){
        return "pages/dept";
    }
    @RequestMapping("addDept")
    public String addDept(Integer type, Integer parentId, Model model){
        model.addAttribute("type",type+1);
        model.addAttribute("parentId",parentId);
        return "pages/addDept";
    }

    @RequestMapping("dept/addDeptSubmit")
    @ResponseBody
    public R addDept(Dept dept){
        //创建记录时间
        dept.setCreateTime(new Date());
        deptServiceImpl.addDept(dept);
        return R.ok();
    }

    @RequestMapping("dept/updateDeptSubmit")
    @ResponseBody
    public R updateDept(Dept dept){
        //创建修改时间
        dept.setUpdateTime(new Date());
        deptServiceImpl.updateDept(dept);
        return R.ok();
    }

    @RequestMapping("dept/deleteDeptByID")
    @ResponseBody
    public R deleteDeptById(int id){
        deptServiceImpl.deleteDeptById(id);
        return R.ok();
    }
    /**
     * 部门树结构的渲染工作
     */
    @RequestMapping("queryDeptTree")
    @ResponseBody
    public Object queryDeptTree(){
        return deptServiceImpl.queryDeptTree();
    }

}
